package javacommon.jfinal.config;

import java.util.Properties;

import javacommon.authority.controller.DicDataController;
import javacommon.authority.controller.IndexController;
import javacommon.authority.controller.MenuController;
import javacommon.authority.controller.OrgController;
import javacommon.authority.controller.RoleController;
import javacommon.authority.controller.UserController;
import javacommon.authority.model.DicData;
import javacommon.authority.model.Menu;
import javacommon.authority.model.Org;
import javacommon.authority.model.Role;
import javacommon.authority.model.User;
import javacommon.authority.model.UserOrg;
import javacommon.authority.model.UserRole;

import com.jagregory.shiro.freemarker.ShiroTags;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.i18n.I18N;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.tx.TxByActionMethods;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.FreeMarkerRender;

import freemarker.template.TemplateModelException;

/**
 * API引导式配置
 */
public class FinalConfig extends JFinalConfig {

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		loadPropertyFile("a_little_config.txt");
		me.setDevMode(getPropertyToBoolean("devMode", false));
		I18N.init("message", null, null);
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class, "/WEB-INF/freemarker");
		me.add("/menu", MenuController.class);
		me.add("/system/org", OrgController.class,"/WEB-INF/freemarker/system/org");
		me.add("/system/user", UserController.class,"/WEB-INF/freemarker/system/user");
		me.add("/system/role", RoleController.class,"/WEB-INF/freemarker/system/role");
		me.add("/system/dicdata", DicDataController.class);
		
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),
				getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);

		//添加Spring支持
//		me.add(new SpringPlugin());
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		arp.setDialect(new MysqlDialect());
		// arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型
		arp.addMapping("t_user", User.class);
		arp.addMapping("t_menu", Menu.class);
		arp.addMapping("t_org", Org.class);
		arp.addMapping("t_role", Role.class);
		arp.addMapping("t_user_role", UserRole.class);
		arp.addMapping("t_user_org", UserOrg.class);
		arp.addMapping("t_dic_data", DicData.class);
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new TxByActionMethods("save", "update", "delete"));
		me.add(new SessionInViewInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {

	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		try {
			//加载属性文件
			Properties p = loadPropertyFile("freemarker.properties");
			FreeMarkerRender.setProperties(p);
			// 设置全局变量
			FreeMarkerRender.getConfiguration().setSharedVariable("ctx",
					JFinal.me().getServletContext().getContextPath());
			FreeMarkerRender.getConfiguration().setSharedVariable("shiro", new ShiroTags());
		} catch (TemplateModelException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目 运行此 main
	 * 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 80, "/", 5);
	}
}
